import WxValidate from '../../utils/WxValidate';
// pages/entryWant/entryWant.js  我要报单

//定义本页面
let app = getApp();
let that;

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */  
  onLoad: function (options) {   //options 页面传来的参数
    //console.log("ssssss",options);
    
    that = this;  //获取本页面
    that.setData({
      "workOrderCode": {
        "title": "工单号",
        "id": "workOrderCode",
        "value": "SJ-2018110797470678502",
        "borderClass": "false"  //是否需要边框
      },
      "occurTime": {
        "title": "发生时间",
        "id": "occurTime",
        "value": "2018-11-07 15:28:37",
        "borderClass": "false"  //是否需要边框
      },
      "imageUploaded":{
        "id":'imageUploaded'
      },
      "processTitle": {
        "title": "标题",
        "id": "processTitle",
        "value": "",
        "borderClass": "true"
      },
      "source": {
        "array": ["电话", "短信", "邮件", "自服务台", "其他", "自主报障"],
        "title": "事件来源",
        "id": "source",
        "index": 1
      },
      "category": {
        "array": ["服务器", "路由器", "打印机", "交换机", "数据库服务器", "硬件", "软件"],
        "title": "类别",
        "id": "category",
        "index": ''
      },
      "summary": {
        "title": "摘要",
        "id": "summary",
        "value": "",
        "borderClass": "true"
      },
      "description": {
        "title": "描述",
        "id": "description", 
        "value": "",
      },
      "urgent":{
        "array": ["轻微", "一般", "紧急", "非常紧急"],
        "title": "紧急程度",
        "id": "urgent",
        "index": ''
      },
      "impact": {
        "array": ["影响个人", "影响小组", "影响部门", "影响公司", "影响集团业务"],
        "title": "影响程度",
        "id": "impact",  
        "index": ''
      },
      "isMajor": {
        "name": "isMajor",
        "tf": "true",   //默认选择
        "array": [
          { "name": "true", "value": "是", "checked": "true" },
          { "name": "false", "value": "否" }
        ]
      },
      "officePhone": {
        "title": "办公电话",
        "id": "officePhone",
        "value": "",
        "borderClass": "true"
      },
      "dept": {
        "title": "部门",
        "id": "dept",
        "value": "",
        "borderClass": "true"
      },
      "post": {
        "title": "职位",
        "id": "post",
        "value": "",
        "borderClass": "true"
      },
      "mobile": {
        "title": "手机",
        "id": "mobile",
        "value": "",
        "borderClass": "true"
      },
      "Email": {
        "title": "Email",
        "id": "email",
        "value": "",
        "borderClass": "true"
      },
      "applicantId": {
        "title": "申报人",
        "id": "applicantId",
        "value": "",
        "borderClass": "true"
      },
      "nextActivityId":{
        "title": "主办人",
        "id": "nextActivityId",
        "value": {                   //当前列表中选中的radio 的id 与值
          "idArray": "",
          "nameArray": ""
        }
      },
      "nextActPersonId": {
        "title": "经办人",
        "id": "nextActPersonId",
        "value": {                   //当前列表中选中的radio 的id 与值
          "idArray": "",
          "nameArray": ""
        }
      }
    });


    
    //表单-验证字段 初始化
    that.initValidate();
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("页面显示");

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("页面隐藏")

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

    console.log("页面卸载")
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  

  //点击button弹出msg
  bottomAlert: function(e){
    console.log("调用成功");
    wx.showToast({
      title: '失败',
      icon: 'succes',
      duration: 1000,
      mask: true      
    })
    wx.showModal({
      title: '提示',
      content: '这是一个模态弹窗',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //表单提交
  formSubmit: function (re) {

    // console.log(re.detail.value);
    // 传入表单数据，调用验证方法
    console.log(re.detail.value);
    const params = re.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      app.showModal(error)
      return false
    }else{
      console.log('form', re.detail.value, 'images', that.data.images)
    }
  },

  //表单-验证字段
  initValidate(){

    // 验证字段的规则
    const rules = {
      processTitle: {   //标题
        required: true,
        minlength: 5
      },
      source:{   //事件来源
        required: true,
      },
      category: {   //类别
        required: true,
      },
      summary:{  //摘要
        required: true,
        minlength: 5
      },
      description: {  //描述
        required: true,
        rangelength: [5, 200]
      },
      urgent:{  //紧急程度
        required: true,
      },
      impact:{  //影响程度
        required: true,
      }

    }

    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      processTitle: {
        required: '请输入标题',
        minlength: '最少要输入 5 个字符'
      },
      source: {   //事件来源
        required: '请选择事件来源',
      },
      category: {
        required: '请选择类别',
      },
      summary: {
        required: "请输入摘要",
        minlength: '最少要输入 5 个字符'
      },
      description: {
        required: "请输入描述",
        rangelength: '请输入长度在 5 到 200 之间的字符。'
      },
      urgent:{
        required: '请选择紧急程度',
      },
      impact:{
        required: '请选择影响程度',
      },
    }

    // 创建实例对象
    that.WxValidate = new WxValidate(rules, messages);

  }


})